package com.sanmina.route.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.sanmina.route.entity.Location;
import com.sanmina.route.entity.Route;
import com.sanmina.route.entity.RouteStep;

public class RouteDaoImpl implements RouteDao {
	// private static Connection conn = null;

	public synchronized static Connection getConnection() {
		String url = "jdbc:postgresql://127.0.0.1:5432/postgres";
		String user = "postgres";
		String pass = "admin";
		Connection conn = null;
		try {
			Class.forName("org.postgresql.Driver");
			conn = DriverManager.getConnection(url, user, pass);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		}

		return conn;
	}

	@Override
	public List<Location> listAllLocations() {
		List<Location> locations = new ArrayList<Location>();
		String sql = "SELECT \"location-key\",workstation FROM location ORDER BY workstation";
		PreparedStatement pstat = null;
		ResultSet rs = null;
		Connection conn = getConnection();
		try {
			pstat = conn.prepareStatement(sql);

			rs = pstat.executeQuery();
			while (rs.next()) {
				Location location = new Location();
				location.setLocationKey(rs.getInt(1));
				location.setWorkstation(rs.getString(2));
				locations.add(location);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (pstat != null) {
					pstat.close();
				}
				if (conn != null) {
					conn.close();
				}

			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return locations;
	}

	@Override
	public boolean addRoute(Route route) {
		int i = 0;
		String sql = "INSERT INTO route(name) VALUES(?)";
		PreparedStatement pstat = null;
		ResultSet rs = null;
		Connection conn = getConnection();
		try {
			pstat = conn.prepareStatement(sql);
			pstat.setString(1, route.getName());
			i = pstat.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (pstat != null) {
					pstat.close();
				}
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return i > 0;
	}

	@Override
	public boolean updateRouteStepsNumber(Integer routeKey, Integer stepsNum) {
		String sql = "UPDATE route SET num_steps=? WHERE \"route-key\" = ? ";
		int i = 0;
		PreparedStatement pstat = null;
		ResultSet rs = null;
		Connection conn = getConnection();
		try {
			pstat = conn.prepareStatement(sql);
			pstat.setInt(1, stepsNum);
			pstat.setInt(2, routeKey);
			i = pstat.executeUpdate();
		} catch (Exception e) {

			e.printStackTrace();
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (pstat != null) {
					pstat.close();
				}
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return i > 0;
	}

	@Override
	public List<Location> searchLoactionByName(String workstation) {
		List<Location> locations = new ArrayList<Location>();
		String sql = "SELECT \"location-key\",workstation FROM location WHERE workstation like ? ORDER BY workstation";
		PreparedStatement pstat = null;
		ResultSet rs = null;
		Connection conn = getConnection();
		try {
			pstat = conn.prepareStatement(sql);
			pstat.setString(1, "%" + workstation + "%");
			rs = pstat.executeQuery();
			while (rs.next()) {
				Location location = new Location();
				location.setLocationKey(rs.getInt(1));
				location.setWorkstation(rs.getString(2));
				locations.add(location);
			}
		} catch (Exception e) {

			e.printStackTrace();
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (pstat != null) {
					pstat.close();
				}
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		return locations;
	}

	@Override
	public List<Route> listAllRoute() {
		List<Route> routes = new ArrayList<Route>();
		String sql = "SELECT * FROM route WHERE num_steps != 0 ORDER BY name";
		PreparedStatement pstat = null;
		ResultSet rs = null;
		Connection conn = getConnection();
		try {
			pstat = conn.prepareStatement(sql);
			rs = pstat.executeQuery();
			while (rs.next()) {
				Route route = new Route();
				route.setRouteKey(rs.getInt(1));
				route.setName(rs.getString(3));
				route.setStepsNum(rs.getInt(6));
				routes.add(route);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (pstat != null) {
					pstat.close();
				}
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return routes;
	}

	@Override
	public List<Route> searchRouteByName(String name) {
		List<Route> routes = new ArrayList<Route>();
		String sql = "SELECT * FROM route WHERE name like ? and num_steps != 0 ORDER BY name";
		PreparedStatement pstat = null;
		ResultSet rs = null;
		Connection conn = getConnection();
		try {
			pstat = conn.prepareStatement(sql);
			pstat.setString(1, "%" + name + "%");
			rs = pstat.executeQuery();
			while (rs.next()) {
				Route route = new Route();
				route.setRouteKey(rs.getInt(1));
				route.setName(rs.getString(3));
				route.setStepsNum(rs.getInt(6));
				routes.add(route);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (pstat != null) {
					pstat.close();
				}
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return routes;
	}

	@Override
	public Route searchRouteByrouteKey(Integer routeKey) {
		Route route = new Route();
		String sql = "SELECT \"route-key\" ,name, num_steps FROM route WHERE \"route-key\" = ?";
		PreparedStatement pstat = null;
		ResultSet rs = null;
		Connection conn = getConnection();
		try {
			pstat = conn.prepareStatement(sql);
			pstat.setInt(1, routeKey);
			rs = pstat.executeQuery();
			while (rs.next()) {
				route.setRouteKey(rs.getInt(1));
				route.setName(rs.getString(2));
				route.setStepsNum(rs.getInt(3));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (pstat != null) {
					pstat.close();
				}
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return route;
	}

	public Route isRouteExist(String name) {
		Route route = new Route();
		String sql = "SELECT * FROM route WHERE name = ? ORDER BY name";
		PreparedStatement pstat = null;
		ResultSet rs = null;
		Connection conn = getConnection();
		try {
			pstat = conn.prepareStatement(sql);
			pstat.setString(1, name);
			rs = pstat.executeQuery();
			while (rs.next()) {
				route.setRouteKey(rs.getInt(1));
				route.setName(rs.getString(3));
				route.setStepsNum(rs.getInt(6));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (pstat != null) {
					pstat.close();
				}
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return route;
	}

	@Override
	public boolean addRouteStep(RouteStep step) {
		int i = 0;
		String sql = "INSERT INTO route_step(\"route-key\",step,\"location-key\",\"pass_to_loc-key\",\"func_fail_to_loc-key\",\"location1-key\",\"location2-key\",\"location3-key\",\"location4-key\",create_unit,inserted_route) VALUES(?,?,?,?,?,?,?,?,?,?,?)";
		PreparedStatement pstat = null;
		ResultSet rs = null;
		Connection conn = getConnection();
		try {
			pstat = conn.prepareStatement(sql);
			pstat.setInt(1, step.getRouteKey());
			pstat.setInt(2, step.getStep());
			pstat.setInt(3, step.getLocationKey());
			pstat.setInt(4, step.getPassLoctionKey());
			pstat.setInt(5, step.getFailLocationKey());
			pstat.setInt(6, step.getFromLocationKey1());
			pstat.setInt(7, step.getFromLocationKey2());
			pstat.setInt(8, step.getFromLocationKey3());
			pstat.setInt(9, step.getFromLocationKey4());
			pstat.setInt(10, step.getCreateUnit());
			pstat.setInt(11, step.getInsertedRoute());
			i = pstat.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (pstat != null) {
					pstat.close();
				}
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		return i > 0;
	}

	@Override
	public List<RouteStep> listAllRouteStepsByRouteKey(Integer routekey) {
		List<RouteStep> routeSteps = new ArrayList<RouteStep>();

		String sql = "SELECT * FROM route_step WHERE \"route-key\" = ? ORDER BY step";
		PreparedStatement pstat = null;
		ResultSet rs = null;
		Connection conn = getConnection();
		try {
			pstat = conn.prepareStatement(sql);
			pstat.setInt(1, routekey);
			rs = pstat.executeQuery();
			while (rs.next()) {
				RouteStep routeStep = new RouteStep();
				routeStep.setRouteKey(rs.getInt(1));
				routeStep.setStep(rs.getInt(2));
				routeStep.setInsertedRoute(rs.getInt(3));
				routeStep.setLocationKey(rs.getInt(4));
				routeStep.setPassLoctionKey(rs.getInt(5));
				routeStep.setFailLocationKey(rs.getInt(7));
				routeStep.setFromLocationKey1(rs.getInt(8));
				routeStep.setFromLocationKey2(rs.getInt(9));
				routeStep.setFromLocationKey3(rs.getInt(10));
				routeStep.setFromLocationKey4(rs.getInt(11));
				routeStep.setCreateUnit(rs.getInt(12));
				routeSteps.add(routeStep);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null) {

					rs.close();
				}
				if (pstat != null) {
					pstat.close();
				}
				if (conn != null) {
					conn.close();
					conn = null;
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (routeSteps.isEmpty()) {
			return null;
		} else {
			return routeSteps;
		}
	}

	@Override
	public Location searchLocationByKey(Integer locKey) {
		Location location = new Location();
		String sql = "SELECT \"location-key\", workstation FROM location WHERE \"location-key\" = ?";
		PreparedStatement pstat = null;
		ResultSet rs = null;
		Connection conn = getConnection();
		try {
			pstat = conn.prepareStatement(sql);
			pstat.setInt(1, locKey);
			rs = pstat.executeQuery();
			while (rs.next()) {
				location.setLocationKey(rs.getInt(1));
				location.setWorkstation(rs.getString(2));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null) {

					rs.close();
				}
				if (pstat != null) {
					pstat.close();
				}
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return location;
	}

	@Override
	public boolean isRouteStepExist(Integer routeKey) {
		String sql = "SELECT * FROM route_step where \"route-key\" = ?";

		return false;
	}

	@Override
	public boolean deleteRouteStepByRouteKey(Integer routeKey) {
		String sql = "DELETE FROM route_step where \"route-key\" = ?";
	
		int i = 0;
		PreparedStatement pstat = null;
		Connection conn = getConnection();
		try {
			pstat = conn.prepareStatement(sql);
			pstat.setInt(1, routeKey);
			i=pstat.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				if (pstat != null) {
					pstat.close();
				}
				if (conn != null) {
					conn.close();
					conn = null;
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return i>0;
	}

	public static void main(String[] args) {
		RouteDao dao = new RouteDaoImpl();
		Route route = new Route();
		route.setName("Route1");
		System.out.println(dao.deleteRouteStepByRouteKey(1));
	}

}
